//
//  ViewController.m
//  ImagePickerTest
//
//  Created by iptv-macmini on 2020/10/21.
//

#import "ViewController.h"
#import <PhotosUI/PhotosUI.h>
#import <Photos/Photos.h>
#import "ImageLibViewController.h"

@interface ViewController ()<PHPickerViewControllerDelegate>
/**
 *  相册，图库选择器
 */
@property (nonatomic, strong) PHPickerViewController *pickerVC;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite];
    if (status == PHAuthorizationStatusLimited) {
        self.choiceSelectBtn.enabled = YES;
    }else{
        self.choiceSelectBtn.enabled = NO;
    }
}

- (IBAction)choiceSelectAct:(id)sender {
    [[PHPhotoLibrary sharedPhotoLibrary] presentLimitedLibraryPickerFromViewController:self];
}

- (IBAction)choiceImgAct:(id)sender {
    [PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^(PHAuthorizationStatus status) {
        //系统相册
        switch (status) {
            case PHAuthorizationStatusNotDetermined:
            case PHAuthorizationStatusRestricted:
            case PHAuthorizationStatusDenied:
            {

            }
                break;
            case PHAuthorizationStatusAuthorized:
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    //PHPickerConfiguration原生相册带搜索功能
                    PHPickerConfiguration *config = [[PHPickerConfiguration alloc] initWithPhotoLibrary:[PHPhotoLibrary sharedPhotoLibrary]];
                    config.filter = [PHPickerFilter imagesFilter];
                    config.selectionLimit = 1;
                    self.pickerVC = [[PHPickerViewController alloc] initWithConfiguration:config];
                    self.pickerVC.delegate = self;
                    self.pickerVC.modalPresentationStyle = UIModalPresentationFullScreen;
                    [self presentViewController:self.pickerVC animated:YES completion:^{}];
                    //ImageLibViewController自定义相册
//                    ImageLibViewController *imgLib = [[ImageLibViewController alloc] init];
//                    [self.navigationController pushViewController:imgLib animated:YES];
                });
            }
                break;
            case PHAuthorizationStatusLimited:
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    ImageLibViewController *imgLib = [[ImageLibViewController alloc] init];
                    [self.navigationController pushViewController:imgLib animated:YES];
                });
            }
                break;
            default:
                break;
        }
    }];
}

- (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results
{
    if (results.count<=0) {
        
    }else{
        PHPickerResult *imgResult = [results objectAtIndex:0];
        if ([imgResult.itemProvider canLoadObjectOfClass:UIImage.class]) {
            [imgResult.itemProvider loadObjectOfClass:[UIImage class] completionHandler:^(__kindof id<NSItemProviderReading>  _Nullable object, NSError * _Nullable error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIImage *image = object;
                    self.showImageView.image = image;
                });
            }];
        }
        
    }
    [self.pickerVC dismissViewControllerAnimated:YES completion:nil];
}
@end
